package day03;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class A00 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过Cookie记录最后的访问时间,并显示到浏览器上
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().write("你就不要想起我");
        //  1).获取系统当前时间毫秒值
        long l = System.currentTimeMillis();
        //  2).创建cookie对象
        Cookie cookie = new Cookie("time",l+"");
        //  3).设置cookie存活时间
        cookie.setMaxAge(60*60);
        //  4).将cookie对象添加到响应对象中
        resp.addCookie(cookie);
        //  5).获取所有cookie 对象
        Cookie[] cookies = req.getCookies();
        //遍历cookies数组
        for (Cookie cook : cookies) {
            //判断是否存储了时间的cookie
            if (cook.getName().equals("time")){
                String value = cook.getValue();
                //使用时间解析  将毫秒值转换成 yyyy-MM-dd HH:mm:ss
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                //将字符串转成long类型
                long time = Long.parseLong(value);
                Date date = new Date(time);
                String format = sdf.format(date);
                System.out.println(format);
                resp.getWriter().write(format);
            }

        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
    }
}
